package com.authsphere.security.account.api.lock;

import java.io.Serial;
import java.io.Serializable;
import java.time.Instant;
import java.time.LocalDateTime;

/**
 * @program: AuthSphere
 * @description:
 * @author: YuKai Fan
 * @create: 2025/3/14 09:30
 **/
public class LockedAccount implements Serializable {
    @Serial
    private static final long serialVersionUID = 2022185526509452820L;

    /**
     * username
     */
    private String username;

    /**
     * failed attempts
     */
    private int failedAttempts;

    /**
     * last failed time
     */
    private long lastFailedTime;

    /**
     * 是否锁定
     */
    private boolean lockedPermanently;

    /**
     * rest unlock seconds
     */
    private long unlockTime;

    /**
     * 锁定分钟数
     */
    private int lockMinutes;

    /**
     * 剩余次数
     */
    private int restAttempts;

    public LockedAccount() {
        this.failedAttempts = 0;
    }

    public LockedAccount(String username) {
        this.username = username;
        this.failedAttempts = 0;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getFailedAttempts() {
        return failedAttempts;
    }

    public void setFailedAttempts(int failedAttempts) {
        this.failedAttempts = failedAttempts;
    }

    public long getLastFailedTime() {
        return lastFailedTime;
    }

    public void setLastFailedTime(long lastFailedTime) {
        this.lastFailedTime = lastFailedTime;
    }

    public long getUnlockTime() {
        return unlockTime;
    }

    public void setUnlockTime(long unlockTime) {
        this.unlockTime = unlockTime;
    }

    public boolean isLocked() {
        return unlockTime != 0 && unlockTime > Instant.now().toEpochMilli();
    }

    public int getRestAttempts() {
        return restAttempts;
    }

    public void setRestAttempts(int restAttempts) {
        this.restAttempts = restAttempts;
    }

    public int incrementFailedAttempts() {
        return this.failedAttempts++;
    }


    public boolean isLockedPermanently() {
        return lockedPermanently;
    }

    public void setLockedPermanently(boolean lockedPermanently) {
        this.lockedPermanently = lockedPermanently;
    }

    public int getLockMinutes() {
        return lockMinutes;
    }

    public void setLockMinutes(int lockMinutes) {
        this.lockMinutes = lockMinutes;
    }
}
